package com.currency.mall.ui.main;

import com.currency.mall.Constant;
import com.currency.mall.entity.BannerBean;
import com.currency.mall.entity.OptimalOrderBean;
import com.currency.mall.entity.SecurityBean;
import com.currency.mall.entity.SharesBean;
import com.currency.mall.netWork.NetWork;
import com.currency.mall.ui.main.optional.OptionalContract;
import com.mifengs.commonbase.util.ToastUitl;
import com.mifengs.commonbusines.entity.ResponseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/12/26.
 */

public class HomeFragmentPresent implements HomeFragmentContract.Presenter {
    private HomeFragmentContract.View mView;

    public HomeFragmentPresent(HomeFragmentContract.View mView) {
        this.mView = mView;
    }

    @Override
    public void securityInfo() {
        NetWork.getSharesApi().securityInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<SecurityBean>bindToLife())
                .subscribe(new Observer<SecurityBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.finishRefresh();
                    }

                    @Override
                    public void onNext(SecurityBean securityBean) {
                        mView.finishRefresh();
                        if(securityBean.getCode().equals(Constant.SUCCESS_CODE)){
                            mView.setSecurityData(securityBean.getBonds());
                        }else{
                            ToastUitl.show(securityBean.getMsg(),200);
                        }

                    }
                });
    }

    @Override
    public void getOptimalOrder() {
        NetWork.getHomeApi().getOptimalOrder()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<OptimalOrderBean>bindToLife())
                .subscribe(new Observer<OptimalOrderBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.finishRefresh();
                    }

                    @Override
                    public void onNext(OptimalOrderBean optimalOrderBean) {
                        mView.finishRefresh();
                        if(optimalOrderBean.getCode().equals(Constant.SUCCESS_CODE)){
                            mView.setOptimalOrder(optimalOrderBean.getOptimalOrders());
                        }else{
                            ToastUitl.show(optimalOrderBean.getMsg(),200);
                        }

                    }
                });
    }

    @Override
    public void getBanner() {
        NetWork.getHomeApi().getBanner()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<BannerBean>bindToLife())
                .subscribe(new Observer<BannerBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.finishRefresh();
                    }

                    @Override
                    public void onNext(BannerBean bannerBean) {
                        mView.finishRefresh();
                        if(bannerBean.getCode().equals(Constant.SUCCESS_CODE)){
                            List<BannerBean> list = new ArrayList<>();
                            list.add(bannerBean);
                            mView.setBanner(list);
                        }else{
                            ToastUitl.show(bannerBean.getMsg(),200);
                        }

                    }
                });

    }
}
